#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest
@File ：mirror_servers.py
@Author ：admin
@Date ：2024/3/21 11:35 
"""
from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getDetail, _getOperationType
from kuberCloudTest.exception import KuberCloudException
from kuberCloudTest.urls import Urls


class mirrorServerUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return mirrorServerUtils(accessToken=accessToken)


def getMirrorServer(accessToken, mirrorServerId=None, **kwargs):
    """
    获取镜像服务器
    :param mirrorServerId: 镜像服务器uuid
    :param accessToken: access token
    :param kwargs: limit:
                   offset:
                   keyword: xxx
                   searchItem: 筛选类型 0: name; 1: uuid;
    :return:
    """
    content = get_utils(accessToken).cloud_http_request.get(Urls.getMirrorServer.value,
                                                            {
                                                                'offset': kwargs.get('offset', OFFSET),
                                                                'limit': kwargs.get('limit', LIMIT),
                                                                'searchitem': kwargs.get('searchItem', 0),
                                                                'keyword': kwargs.get('keyword', '')
                                                            })
    if mirrorServerId:
        total = content.get('total', 0)
        content = get_utils(accessToken).cloud_http_request.get(
            Urls.getMirrorServer.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, mirrorServerId, 'msid')
    return content


def startMirrorServer(accessToken, mirrorServerIds):
    """
    启用镜像服务器
    :param accessToken: access token
    :param mirrorServerIds: 镜像服务器uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 2004
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = mirrorServerIds
    return get_utils(accessToken).cloud_http_request.post(Urls.startMirrorServer.value, data=data)


def stopMirrorServer(accessToken, mirrorServerIds):
    """
    停用镜像服务器
    :param accessToken: access token
    :param mirrorServerIds: 镜像服务器uuid (多个使用英文逗号分割)
    :return:
    """
    operationType = 2003
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = mirrorServerIds
    return get_utils(accessToken).cloud_http_request.post(Urls.stopMirrorServer.value, data=data)


def createMirrorServer(accessToken):
    """
    创建镜像服务器
    :param accessToken:
    :return:
    """
    pass


def updateMirrorServer(accessToken, mirrorServerId, mirrorServerName, remark=''):
    """
    更新镜像服务器
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :param mirrorServerName: 镜像服务器名称
    :param remark: 备注
    :return:
    """
    mirrorServer = getMirrorServer(accessToken=accessToken, mirrorServerId=mirrorServerId).get('obj', None)
    if not mirrorServer:
        raise KuberCloudException('params error', 'mirror [{}] not exist'.format(mirrorServer))
    operationType = 2007
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['id'] = mirrorServerId
    data['name'] = mirrorServerName
    data['remark'] = remark if remark else mirrorServer['remark']

    return get_utils(accessToken).cloud_http_request.post(Urls.updateMirrorServer.value, data=data)


def deleteMirrorServer(accessToken, mirrorServerId):
    """
    删除镜像服务器
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :return:
    """
    operationType = 2002
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['id'] = mirrorServerId
    return get_utils(accessToken).cloud_http_request.post(Urls.deleteMirrorServer.value, data=data)


def mirrorServerEnterMaintainMode(accessToken, mirrorServerId):
    """
    镜像服务器进入维护模式
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :return:
    """
    operationType = 2010
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = mirrorServerId
    return get_utils(accessToken).cloud_http_request.post(Urls.mirrorServerEnterMaintainMode.value, data=data)


def updateMirrorServerGlusterFsConf(accessToken, mirrorServerId, url, mountParam):
    """
    更新镜像服务器GlusterFS配置
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :param url: GlusterFs存储URL，如：192.168.20.177:vol1
    :param mountParam: 挂载参数，如：
    :return:
    """
    operationType = 2013
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['msid'] = mirrorServerId
    data['url'] = url
    data['mountparam'] = mountParam
    return get_utils(accessToken).cloud_http_request.post(Urls.updateMirrorServerGlusterFsConf.value, data=data)


def updateMirrorServerCephFsConf(accessToken, mirrorServerId, url, cephName, secret, specifyFs):
    """
    更新镜像服务器CephFS配置
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :param url: CephFS存储URL，如：192.168.20.210:8000:/
    :param cephName: CephFS存储名称，如：admin
    :param secret: CephFS存储密钥，如：AQCf2KZhhnA6MBAAdMS0rgH2VpMIooo7jwhDuw==
    :param specifyFs: CephFS存储挂载点，如：cephfs
    :return:
    """
    operationType = 2012
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['msid'] = mirrorServerId
    data['url'] = url
    data['cephname'] = cephName
    data['secret'] = secret
    data['specifyfs'] = specifyFs
    return get_utils(accessToken).cloud_http_request.post(Urls.updateMirrorServerCefsFsConf.value, data=data)


def getStopVMsForMirrorServer(accessToken, mirrorServerId):
    """
    获取镜像服务器停用关联虚拟机
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :return:
    """
    operationType = 2014
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['Id'] = mirrorServerId
    return get_utils(accessToken).cloud_http_request.post(Urls.getStopVMsForMirrorServer.value, data=data)


def cleanMirrorServer(accessToken, mirrorServerId):
    """
    清理镜像服务器废弃数据
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :return:
    """
    operationType = 2006
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['Id'] = mirrorServerId
    return get_utils(accessToken).cloud_http_request.post(Urls.cleanMirrorServer.value, data=data)


def reconnectMirrorServer(accessToken, mirrorServerId):
    """
    重新连接镜像服务器
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :return:
    """
    operationType = 2005
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['Id'] = mirrorServerId
    return get_utils(accessToken).cloud_http_request.post(Urls.reconnectMirrorServer.value, data=data)


def mirrorServerSyncMonNode(accessToken, mirrorServerId, cephHost, cephPort, cephUser, cephPwd):
    """
    同步镜像服务器MON节点
    :param accessToken:
    :param mirrorServerId: 镜像服务器uuid
    :param cephHost: Ceph服务器IP，如：192.168.20.210
    :param cephPort: Ceph服务器端口，如：6789
    :param cephUser: Ceph服务器用户名
    :param cephPwd: Ceph服务器密码
    :return:
    """
    operationType = 2011
    data = _getOperationType(operationType, AsyncType.ASYNC.value)
    data['msid'] = mirrorServerId
    data['hostIp'] = cephHost
    data['sshPort'] = cephPort
    data['userName'] = cephUser
    data['pwd'] = cephPwd
    return get_utils(accessToken).cloud_http_request.post(Urls.mirrorServerSyncMonNode.value, data=data)
